ProcRestarting = class("ProcRestarting", GameStateBase)
ProcRestarting.IsRestarting = false

function ProcRestarting:onBegin()
    ProcRestarting.IsRestarting = true

    --打开遮罩
    if not LoadingSceneOpen then
        local go = GameObject.Instantiate(resMgr:LoadAsset("Res/Internal/Prefab/ui/Login/Loading.prefab"))
        go.name = "LoadingBg";
        local parent = GameObject.Find("init").transform:Find("ScreenCanvas2D/Loading")
        go.transform:SetParent(parent, false);
    end

    coroutine.stopAllCoroutine()
    coroutine.start(function()
        for _, callback in ipairs(OnGameRestart) do
            callback()
        end

        --重新请求热更信息
        local urlConfig = require('cjson').decode(VersionInfo.urlConfigStr)
        local _server = ''
        if IsTest then
            _server = urlConfig['downLoadServerUrl_test']
        else
            _server = urlConfig['downLoadServerUrl']
        end

        local verReqUrl = string.format("%s?game=%s&coreversion=%s&cppversion=%s&resversion=%s&client_version=%s", _server .. "resource2.php", BundleID, VersionInfo.coreVersion, VersionInfo.resVersion, VersionInfo.localPatchVer, VersionInfo.cppVersion);

        local www = UnityEngine.Networking.UnityWebRequest(verReqUrl)
        local dH = UnityEngine.Networking.DownloadHandlerBuffer.New();
        www.downloadHandler = dH
        www.timeout = 60
        www:SendWebRequest()
        print('Begin Request: ', verReqUrl)
        coroutine.www(www)
        VersionInfo.onlineVersionInfo = www.downloadHandler.text
        print('Request Finish: ', VersionInfo.onlineVersionInfo)
        StaticResUpdateViewModelInstance:Restart()
        --如果之前有二次下载解压工作，先等解压完成，要不会出现两个线程同时解压的异常情况
        print('VERM.SecondaryDownloadUnzipping: ', VERM.SecondaryDownloadUnzipping)
        coroutine.waitUntil(function() return not VERM.SecondaryDownloadUnzipping end)
        print('VERM.SecondaryDownloadUnzipping finish')
        local loadingContentGO = GameObject.Find('init').transform:Find('ScreenCanvas2D/Loading/loadingContent')
        if utils.IsAvailableGameObject(loadingContentGO) then           --解压完删了进度条
            GameObject.Destroy(loadingContentGO.gameObject)
        end
        --CrashLoggerManager去掉回调
        CrashLoggerManager.Destroy()
        --清空UI
        VIEWM.Destroy()
        --清理加载任务
        RESM.WaitForAllAsyncTaskAsync()
        RESM.Destroy()
        --切换到update状态
        gameMgr:ChangeState("ProcUpdate", {'login'})
    end)
end

function ProcRestarting:OnEnd()
    ProcRestarting.IsRestarting = false
end